// Copyright (C) 2025 Kumo inc.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#ifndef ROTHAR_PEGTL_SRC_TEST_PEGTL_VERIFY_FILE_HPP
#define ROTHAR_PEGTL_SRC_TEST_PEGTL_VERIFY_FILE_HPP

#include <rothar/pegtl.hpp>

#include "test.hpp"

#if defined( _MSC_VER )
#define ROTHAR_PEGTL_TEST_FILENAME u"tests/file_äöü𝄞_data.txt"
#else
#define ROTHAR_PEGTL_TEST_FILENAME "tests/file_äöü𝄞_data.txt"
#endif

namespace rothar
{
   struct file_content
      : seq< ROTHAR_PEGTL_STRING( "dummy content" ), eol, discard >
   {};

   struct file_grammar
      : seq< rep_min_max< 11, 11, file_content >, eof >
   {};

   template< typename Rule >
   struct file_action
   {};

   template<>
   struct file_action< eof >
   {
      static void apply0( bool& flag )
      {
         flag = true;
      }
   };

   template< typename Rule >
   struct file_control
      : normal< Rule >
   {};

   template<>
   struct file_control< eof >
      : normal< eof >
   {
      template< typename ParseInput >
      static void success( const ParseInput& /*unused*/, bool& flag )
      {
         flag = true;
      }
   };

   template< typename T >
   void verify_file()
   {
#if defined( __cpp_exceptions )
      {
         try {
            T in( "tests/no_such_file.txt" );
            ROTHAR_PEGTL_TEST_UNREACHABLE;  // LCOV_EXCL_LINE
         }
         catch( const internal::filesystem::filesystem_error& ) {
         }
      }
#endif

      {
         T in( "tests/file_data.txt" );
         ROTHAR_PEGTL_TEST_ASSERT( in.source() == "tests/file_data.txt" );
         ROTHAR_PEGTL_TEST_ASSERT( parse< file_grammar >( in ) );
         ROTHAR_PEGTL_TEST_ASSERT( in.source() == "tests/file_data.txt" );
      }
      {
         T in( ROTHAR_PEGTL_TEST_FILENAME );
         bool flag = true;
         ROTHAR_PEGTL_TEST_ASSERT( parse< file_grammar >( in, flag ) );
         ROTHAR_PEGTL_TEST_ASSERT( flag == true );
      }
      {
         T in( ROTHAR_PEGTL_TEST_FILENAME );
         bool flag = false;
         ROTHAR_PEGTL_TEST_ASSERT( parse< file_grammar >( in, flag ) );
         ROTHAR_PEGTL_TEST_ASSERT( flag == false );
      }
      {
         T in( ROTHAR_PEGTL_TEST_FILENAME );
         bool flag = false;
         const bool result = parse< file_grammar, file_action >( in, flag );
         ROTHAR_PEGTL_TEST_ASSERT( result );
         ROTHAR_PEGTL_TEST_ASSERT( flag == true );
      }
      {
         T in( ROTHAR_PEGTL_TEST_FILENAME );
         bool flag = false;
         const bool result = parse< file_grammar, nothing, file_control >( in, flag );
         ROTHAR_PEGTL_TEST_ASSERT( result );
         ROTHAR_PEGTL_TEST_ASSERT( flag == true );
      }
      const char* foo = "foo";
      const memory_input m( foo, foo + 3, foo );
      {
         T in( ROTHAR_PEGTL_TEST_FILENAME );
         ROTHAR_PEGTL_TEST_ASSERT( parse_nested< file_grammar >( m, in ) );
      }
      {
         T in( ROTHAR_PEGTL_TEST_FILENAME );
         bool flag = true;
         ROTHAR_PEGTL_TEST_ASSERT( parse_nested< file_grammar >( m, in, flag ) );
         ROTHAR_PEGTL_TEST_ASSERT( flag == true );
      }
      {
         T in( ROTHAR_PEGTL_TEST_FILENAME );
         bool flag = false;
         ROTHAR_PEGTL_TEST_ASSERT( parse_nested< file_grammar >( m, in, flag ) );
         ROTHAR_PEGTL_TEST_ASSERT( flag == false );
      }
      {
         T in( ROTHAR_PEGTL_TEST_FILENAME );
         bool flag = false;
         const bool result = parse_nested< file_grammar, file_action >( m, in, flag );
         ROTHAR_PEGTL_TEST_ASSERT( result );
         ROTHAR_PEGTL_TEST_ASSERT( flag == true );
      }
      {
         T in( ROTHAR_PEGTL_TEST_FILENAME );
         bool flag = false;
         const bool result = parse_nested< file_grammar, nothing, file_control >( m, in, flag );
         ROTHAR_PEGTL_TEST_ASSERT( result );
         ROTHAR_PEGTL_TEST_ASSERT( flag == true );
      }
   }

}  // namespace rothar

#endif
